package com.xxxx.manager.controller;

import com.xxxx.api.service.BrandService;
import com.xxxx.api.service.GoodsCategoryService;
import com.xxxx.api.service.GoodsService;
import com.xxxx.api.pojo.Brand;
import com.xxxx.api.pojo.Goods;
import com.xxxx.api.pojo.GoodsCategory;
import com.xxxx.api.pojo.GoodsCategoryVo;
import com.xxxx.api.utils.RespBean;
import com.xxxx.common.result.BaseResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/*
    商品分类
 */
@Controller
@RequestMapping("goods")
public class GoodsCategoryController {
    @Reference(version = "1.0")
    private GoodsCategoryService goodsCategoryService;
    @Reference(version = "1.0")
    private BrandService brandService;
    @Reference(version = "1.0")
    private GoodsService goodsService;

    /**
     * 页面跳转 商品分类-列表页
     * @return
     */
    @RequestMapping("category/list")
    public String goodsList(Model model){
        List<GoodsCategoryVo> goodsCategoryVos = goodsCategoryService.selectCtegoryListTable();
        model.addAttribute("gcvList",goodsCategoryService.selectCtegoryListTable());
        return "goods/category/goods-list";

    }

    /**
     * 页面跳转 商品分类 -新增页
     * @param model
     * @return
     */
    @RequestMapping("category/add")
    public String goodsAdd(Model model){
        List<GoodsCategory> gcList = goodsCategoryService.selectCategoryTopList();
        model.addAttribute("gcList",gcList);
        return "goods/category/goods-add";
    }

    /**
     * 商品分类-新增分类-级联查询
     * @param parentId
     * @return
     */
    @ResponseBody
    @RequestMapping("category/{parentId}")
    public List<GoodsCategory> selectCategoryList(@PathVariable Short  parentId){
        return goodsCategoryService.selectCategoryByParentId(parentId);

    }

    /**
     * 商品分类 新增分类- 保存分类
     * @param goodsCategory
     * @return
     */
    @RequestMapping("category/save")
    @ResponseBody
    public BaseResult categorySave(GoodsCategory goodsCategory){
     int result = goodsCategoryService.categorySave(goodsCategory);
     //三目判断
        return result > 0 ? BaseResult.success() : BaseResult.error();
    }

    /**
     * 商品列表页面跳转
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list (Model model){
        //返回商品分类
        model.addAttribute("gcList", goodsCategoryService.selectCategoryList());
        //返回商品品牌
        model.addAttribute("brandList", brandService.selectBrandList());
        return "goods/goods-list";
    }

    /**
     * 分页查询接口
     * @param goods
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("listForPage")
    @ResponseBody
    public RespBean listForPage(Goods goods, Integer pageNum, Integer pageSize){
        return goodsService.selectGoods(goods, pageNum, pageSize);
    }

    /**
     * 商品列表添加接口
     * @return
     */
    @RequestMapping("goods-add")
    public String goodsAddList (Model model){
        List<GoodsCategory> topList = goodsCategoryService.selectCategoryTopList();
        model.addAttribute("gcList", topList);
        List<Brand> brandList = brandService.selectBrandList();
        model.addAttribute("brandList", brandList);

        return "goods/goods-add";
    }
}
